//顺序表基本运算算法
#include <stdio.h>
#include <malloc.h>
typedef char SLDataType;
typedef struct SeqList {
    SLDataType* a;
    int size;
    int capacity;
}SL;

void SeqListInit(SL*ps) {
    ps->a = NULL;
    ps->size = ps->capacity = 0;
};

void SeqListCheckCapacity(SeqList*ps) {
    if(ps->size == ps->capacity) {
        int newcapacity = ps->capacity==0?4:ps->capacity*2;
        SLDataType* tmp = (SLDataType*)realloc(ps->a,sizeof(SLDataType)*newcapacity);
        if(tmp == NULL) {
            printf("realloc fail\n");
            exit(-1);
        }
        ps->a = tmp;
        ps->capacity = newcapacity;
    }
}
void SeqListPushback(SL*ps,SLDataType x) {
    SeqListCheckCapacity(ps);
    ps->a[ps->size++] = x;
};
void Display(SL*ps) {
    for(int i = 0;i < ps->size;i++) {
        printf("%c",ps->a[i]);
    }
    printf("\n");
}
void Destory(SL*ps) {
    free(ps->a);
    ps->a = NULL;
}
void SeqListPopBack(SL*ps) {
    if(ps->size>0) {
        ps->a[ps->size-1] = 0;
        ps->size--;
    }
};
void SeqListPushFront(SL*ps,SLDataType x) {
    SeqListCheckCapacity(ps);
    for(int i =ps->size;i>0;i--) {
        ps->a[i] = ps->a[i-1];
    }
    ps->a[0] = x;
    ps->size++;
};
void SeqListPopFront(SL*ps) {
    if(ps->size>0) {
        for(int i =0;i<ps->size-1;i++) {
            ps->a[i] = ps->a[i+1];
        }
        ps->a[ps->size-1]=0;
        ps->size--;
    }
};

void SeqListInsert(SeqList*ps,int i,SLDataType x) {
    SeqListCheckCapacity(ps);
    i--;
    for(int j =ps->size;j>i;j--) {
        ps->a[j]=ps->a[j-1];
    }
    ps->a[i] = x;
    ps->size++;
}
void SeqListErase(SeqList*ps,int i) {
    if(ps->size>0) {
        i--;
        for(int j=i;j<ps->size-1;j++) {
            ps->a[j] = ps->a[j+1];
        }
        ps->size--;
        ps->a[ps->size]=0;
    }
}
int main() {
    SL l;
    SeqListInit(&l);
    SeqListPushback(&l,'a');
    SeqListPushback(&l,'b');
    SeqListPushback(&l,'c');
    SeqListPushback(&l,'d');
    SeqListPushback(&l,'e');
    Display(&l);
    SeqListPopBack(&l);
    Display(&l);
    SeqListPushFront(&l,'0');
    SeqListPushFront(&l,'1');
    SeqListPushFront(&l,'2');
    SeqListPushFront(&l,'3');
    SeqListPushFront(&l,'4');
    SeqListPushFront(&l,'5');
    Display(&l);
    SeqListInsert(&l,3,'A');
    Display(&l);
    SeqListPopFront(&l);
    Display(&l);
    SeqListErase(&l,2);
    Display(&l);
    Destory(&l);
    return 0;
}